$(function() {
    $("input[name='phone']").blur(function () {
        var phone = $(this).val();

        if (formObj.checkNull("phone", "phone不能为空！") && formObj.checkPhone()) {
            $.post("/index/checkphone", {"phone": phone}, function (result) {
                $("#phone_msg").text(result);
                $("#phone_msg").css("color", "red");
            });
        }
    });

    $("input[name='userName']").blur(function () {
        formObj.checkNull("userName", "用户名不能为空！");
    });

    $("input[name='passw1']").blur(function () {
        formObj.checkNull("passw1", "密码不能为空！");
        formObj.checkPassword();

    });

    $("input[name='passw2']").blur(function () {
        formObj.checkNull("passw2", "确认密码不能为空！");
        formObj.checkPassword();
    });

        $("form").submit(function (e) {
            e.preventDefault(); // 阻止表单的默认提交行为

            var flag = true;
            flag = formObj.checkNull("userName", "用户名不能为空！") && flag;
            flag = formObj.checkNull("phone", "电话号码不能为空！") && flag;
            flag = formObj.checkNull("passw1", "密码不能为空！") && flag;
            flag = formObj.checkNull("passw2", "确认密码不能为空！") && flag;
            //flag = formObj.checkPhone() && flag;
            flag = formObj.checkPassword() && flag;

            var userName = $("input[name='userName']").val();
            var phone = $("input[name='phone']").val();
            var password = $("input[name='passw1']").val();
            if (flag) {
                $.post("/register", {"userName": userName, "phone": phone, "password": password}, function (result) {
                    if (result == "注册成功") {
                        alert(result);
                        window.location.href="/toLogin";
                    } else {
                    }
                    $("#form_msg").html(result);
                    $("#form_msg").css("color", "red");
                });
            }
            return false;
        });
    });

    var formObj = {
        "checkPassword": function () {
            var passw1 = $("input[name='passw1']").val();
            var passw2 = $("input[name='passw2']").val();
            if (passw1.length > 0 && passw2.length > 0) {
                if (passw1 != passw2) {
                    this.setMsg("passw1", "两次密码不一致！");
                    this.setMsg("passw2", "两次密码不一致！");
                    return false;
                } else {
                    this.setMsg("passw1", "");
                    this.setMsg("passw2", "");
                }
            }
            return true;
        },
        "checkPhone": function () {
            var phone = $("input[name='phone']").val();
            var regExp = /^\d{11}$/;
            if (phone.length > 0) {
                if (!regExp.test(phone)) {
                    this.setMsg("phone", "phone格式不正确！");
                    return false;
                }
            }
            return true;
        },
        "checkNull": function (name, msg) {
            var value = $("input[name='" + name + "']").val();
            if (value.length == 0) {
                this.setMsg(name, msg);
                return false;
            } else {
                this.setMsg(name, "");
            }
            return true;
        },
        "setMsg": function (name, msg) {
            $("#" + name + "_msg").text(msg);
            $("#" + name + "_msg").css("color", "red");
            //		$("#passw2_msg").css("color","red");
        }
    }


